<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="关键字:" prop="keyword">
        <el-input
          size="mini"
          v-model="queryParams.keyword"
          placeholder="请输入名称/类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:interlaceNoticeScene:add']"
        >新增</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="序号" type="index" align="center" min-width="5%"/>
      <el-table-column label="场景名称" align="center" prop="name" key="name"/>
      <el-table-column label="场景类型" align="center" prop="type" key="type"/>
      <el-table-column label="特定场景" align="center" prop="special" key="special">
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.special === '1'">是</el-tag>
          <el-tag v-else>否</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.special !== '1'"
            type="success"
            plain
            size="small"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:interlaceNoticeScene:edit']"
          >修改
          </el-button>
          <el-button
            v-if="scope.row.special !== '1'"
            type="danger"
            plain
            size="small"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:interlaceNoticeScene:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="场景名称:" prop="name">
          <el-input v-model="form.name" placeholder="请输入场景名称" maxlength="50" />
        </el-form-item>
        <el-form-item label="场景类型:" prop="type">
          <el-input v-model="form.type" placeholder="请输入场景类型" maxlength="50" />
          <div class="el-form-item-tip">提示：建议使用英文和下划线</div>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button size="mini" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {scenePageList, delNotice, addNoticeScene, editNoticeScene, noticeSceneDetail,delNoticeScene} from "@/api/triones/notice";
export default {
  name: "index",
  data() {
    return {
      loading: false,
      showSearch: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyword: undefined
      },
      dataList: [],
      total: 0,
      dateRange: [],
      title: "",
      open: false,
      form: {},
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "类型不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      scenePageList(this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 检索 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除通知场景名称为"' + row.sceneName + '"的数据项?').then(() => {
        this.loading = true;
        return delNoticeScene(id);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        type: undefined,
        special: 0
      };
      this.resetForm("form");
    },
    /** 去新增 */
    handleAdd() {
      this.title = "新增通知场景";
      this.reset();
      this.open = true;
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            editNoticeScene(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNoticeScene(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      noticeSceneDetail(id).then(response => {
        this.form = response.data;
        this.title = "修改通知场景";
        this.open = true;
      });
    }
  }
}
</script>
<style scoped lang="scss">
.el-form-item-tip {
  font-size: 12px;
  color: #c53b3b;
  margin-top: 5px;
}

</style>
